package com.twu.biblioteca.ui.menus;

import com.twu.biblioteca.BibliotecaApp;
import com.twu.biblioteca.app.Library;
import com.twu.biblioteca.app.user.User;

import java.io.BufferedReader;
import java.io.IOException;

public class MainMenu {
    private static final int SHOW_USER_DETAILS_OPTION = 1;
    private static final int LIST_BOOKS_SELECTION_OPTION = 2;
    private static final int LIST_MOVIE_SELECTION_OPTION = 3;
    private static final int RETURN_BOOK_SELECTION_OPTION = 4;
    private static final int QUIT_SELECTION_OPTION = 5;
    private static final String LINE_SEPARATOR = System.getProperty("line.separator");

    private BookListMenu bookListMenu;
    private MovieListMenu movieListMenu;
    private ReturnBookMenu returnBookMenu;
    private BufferedReader bufferedReader;
    private Library library;

    public MainMenu(BufferedReader bufferedReader, Library library) {
        this.library = library;
        this.bufferedReader = bufferedReader;
        movieListMenu = new MovieListMenu(bufferedReader);
        bookListMenu = new BookListMenu(bufferedReader);
        returnBookMenu = new ReturnBookMenu(bufferedReader);
    }

    public String getWelcomeMessage() {
        return("********* Welcome to Biblioteca! *********" + LINE_SEPARATOR);
    }

    public String getMenuOptions() {
        String menuOptions = "";
        menuOptions += "---------------------------------------------" + LINE_SEPARATOR;
        menuOptions += "MENU OPTIONS" + LINE_SEPARATOR;
        menuOptions += "---------------------------------------------" + LINE_SEPARATOR;
        menuOptions += SHOW_USER_DETAILS_OPTION + ". Show My Details" + LINE_SEPARATOR;
        menuOptions += LIST_BOOKS_SELECTION_OPTION + ". List Books" + LINE_SEPARATOR;
        menuOptions += LIST_MOVIE_SELECTION_OPTION + ". List Movies" + LINE_SEPARATOR;
        menuOptions += RETURN_BOOK_SELECTION_OPTION + ". Return Book" + LINE_SEPARATOR;
        menuOptions += QUIT_SELECTION_OPTION + ". Quit" + LINE_SEPARATOR;
        menuOptions += "Please enter a number to select from the options above: ";
        return menuOptions;
    }

    public void readAndHandleMenuSelection(User user) {
        try {
            String input = this.bufferedReader.readLine();
            int selection = Integer.parseInt(input);
            handleMenuSelection(selection, user);
        } catch (NumberFormatException nfe) {
            displaySelectionError();
        } catch (IOException e) {
            System.out.println("There was an error reading from the provided input reader.");
            BibliotecaApp.exit();
        }
    }

    public void handleMenuSelection(int selection, User user) throws IOException {
        switch (selection) {
            case SHOW_USER_DETAILS_OPTION:
                System.out.println(user.getUserDetails());
                printMenuOptions();
                break;
            case LIST_BOOKS_SELECTION_OPTION:
                readAndHandleListLibraryItem(bookListMenu, user);
                break;
            case LIST_MOVIE_SELECTION_OPTION:
                readAndHandleListLibraryItem(movieListMenu, user);
                break;
            case RETURN_BOOK_SELECTION_OPTION:
                System.out.println(returnBookMenu.getMenuOptions());
                library = returnBookMenu.readTitleAndReturnBook(library);
                printMenuOptions();
                break;
            case QUIT_SELECTION_OPTION:
                BibliotecaApp.exit();
                break;
            default :
                displaySelectionErrorAndReprintMenuOptions();
        }
    }

    private void readAndHandleListLibraryItem(ItemListMenu itemListMenu, User user) {
        System.out.println(itemListMenu.getMenuOptions(library));
        library = itemListMenu.readAndHandleMenuSelection(library, user);
        printMenuOptions();
    }

    public void displaySelectionErrorAndReprintMenuOptions() {
        displaySelectionError();
        printMenuOptions();
    }

    private void printMenuOptions() {
        System.out.println(getMenuOptions());
    }

    public void displaySelectionError() {
        System.out.println("Select a valid option!");
    }

    public void displayWelcomeScreen() {
        System.out.print(getWelcomeMessage());
        System.out.print(getMenuOptions());
    }
}
